package controllers

import (
	"cdam/models"
	"cdam/util"
	"errors"
	"math/rand"
	"time"
)

type QiongqiDriverEnginee struct {
}

func (*QiongqiDriverEnginee) start(code string, params map[string]interface{}) error {
	condition := map[string]string{}
	condition["code"] = code
	condition["status"] = "1"
	// 获取流程
	processes, _ := models.GetProcessByCondition(condition)
	if len(processes) > 1 {
		return errors.New("找到的流程不唯一")
	} else if len(processes) == 1 {
		nodeCondition := map[string]string{}
		nodeCondition["processid"] = string(processes[0].Id)
		nodeCondition["code"] = "StartEvent_1"
		nodes, _ := models.GetProcessNodeByCondition(nodeCondition)
		// nodeCondition["code"]
		delete(nodeCondition, "code")

		// 启动流程
		instance := new(models.FlowInstance)
		instance.Id = rand.New(util.Seed).Int63n(100000000)
		instance.Createdate.Time = time.Now()
		instance.Status = "Start"
		instance.ProcessId = processes[0].Id
		instance.LauncherDate.Time = time.Now()
		models.AddFlowInstance(instance)

		// 设置启动流程节点 -- 点击启动按钮 启动节点自动过去
		startNode := new(models.FlowInstanceTask)
		startNode.Id = rand.New(util.Seed).Int63n(100000000)
		startNode.Createdate.Time = time.Now()
		startNode.NodeName = "启动流程"
		startNode.InstanceId = int64(instance.Id)
		startNode.NodeId = int64(nodes[0].Id)
		startNode.Status = "end"
		startNode.Precessor = 1
		models.AddFlowInstanceTask(startNode)
		return nil
	} else {
		return errors.New("没有找到流程")
	}
}
